package com.trade.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.trade.common.util.PaginationSupport;
import com.trade.model.Category;
import com.trade.model.Goods;
import com.trade.service.ICategoryService;
import com.trade.service.IGoodsService;

/**
 * 
 * 处理货物相关的请求
 *
 * @author	chengpeng.zhang
 * @version           1.0
 * @since             2012-11-16
 */
public class GoodsAction extends DispatchAction {
	private static Log logger = LogFactory.getLog(GoodsAction.class);
	private IGoodsService goodsService;
	private ICategoryService categoryService;
	
	public IGoodsService getGoodsService() {
		return goodsService;
	}
	
	public void setGoodsService(IGoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	public ICategoryService getCategoryService() {
		return categoryService;
	}
	
	public void setCategoryService(ICategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	
	/**
	 * 商品信息录入页 
	 * @since  2012-11-16
	 * @author chengpeng.zhang
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goodsin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.debug("goods in request...");
		List<Category> categories = this.categoryService.getCategories();
		request.setAttribute("categorys", categories);
		return mapping.findForward("goodsin");
    }
	/**
	 * 
	 * 商品入库
	 * @since  2012-11-16
	 * @author chengpeng.zhang
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward newGoods(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.debug("newGoods request...");
		String goodsName = request.getParameter("name");
		Goods goods = new Goods();
		goods.setGoodsName(goodsName);
		
		String categoryIdString = request.getParameter("categoryId");
		Category category = categoryService.getCategoryById(Long.valueOf(categoryIdString));
		goods.setCategory(category);
		try {
			this.goodsService.save(goods);
			return mapping.findForward("goodsinSuc");
		} catch (Exception e) {
			request.setAttribute("message", "入库失败");
			logger.debug("入库失败:" + e.getMessage());
			List<Category> categories = this.categoryService.getCategories();
			request.setAttribute("categorys", categories);
			return mapping.findForward("goodsin");
		}
    }
	
	/**
	 * 商品的列表查询
	 * @since  2013-1-27
	 * @author chengpeng.zhang
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goodslist(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.debug("goodslist request...");
		String pageSizeString = request.getParameter("pageSize");
		String pageNoString = request.getParameter("pageNo");
		int pageSize = PaginationSupport.PAGESIZE;
		int pageNo = 1;
		if (StringUtils.isNotEmpty(pageSizeString)) {
			try {
				pageSize = Integer.parseInt(pageSizeString);
			} catch (Exception e) {
				log.warn("wrong page size:" + pageSizeString);
			}
		}
		if (StringUtils.isNotEmpty(pageNoString)) {
			try {
				pageNo = Integer.parseInt(pageNoString);
			} catch (Exception e) {
				log.warn("wrong page number:" + pageNoString);
			}
		}
		int startIndex = (pageNo - 1) * pageSize;
		PaginationSupport page = new PaginationSupport(pageSize, startIndex);
		page = goodsService.getGoodsList(page);
		request.setAttribute("goodspage", page);
		return mapping.findForward("goodslist");
    }
	
}
